﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQData;
using System.Reflection;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for ClassroomHandler
    /// </summary>
    public class eng_ClassroomHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region ClassRoom
            string add_classroom = context.Request["add_classroom"];
            if (!string.IsNullOrEmpty(add_classroom))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_classroom>(add_classroom);
                context.Response.Write("Success Full");
            }

            string list_classroom = context.Request["list_classroom"];
            if (!string.IsNullOrEmpty(list_classroom))
            {
                RenderListClassRoom(context);
            }

            string view_classroom = context.Request["view_classroom"];
            if (!string.IsNullOrEmpty(view_classroom))
            {
                string Id = context.Request["Id"];
                RenderSingleClassRoom(context, Convert.ToInt32(Id));
            }
            string delete_classroom = context.Request["delete_classroom"];
            if (!string.IsNullOrEmpty(delete_classroom))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_classroom>(delete_classroom);
                RenderListClassRoom(context);
            }
            string lockunlock_classroom = context.Request["lockunlock_classroom"];
            if (!string.IsNullOrEmpty(lockunlock_classroom))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_classroom>(lockunlock_classroom, "classroom_active");
                RenderListClassRoom(context);
            }
            #endregion

            #region Register
            string add_register = context.Request["add_register"];
            if (!string.IsNullOrEmpty(add_register))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_register>(add_register);
                context.Response.Write("Success Full");
            }

            string list_register = context.Request["list_register"];
            if (!string.IsNullOrEmpty(list_register))
            {
                RenderListRegister(context);
            }

            string view_register = context.Request["view_register"];
            if (!string.IsNullOrEmpty(view_register))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingleRegister(context, Convert.ToInt32(Id));
                else
                    RenderSelectClassroom(context);
            }

            string delete_register = context.Request["delete_register"];
            if (!string.IsNullOrEmpty(delete_register))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_register>(delete_register);
                RenderListClassRoom(context);
            }
            string lockunlock_register = context.Request["lockunlock_register"];
            if (!string.IsNullOrEmpty(lockunlock_register))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_register>(lockunlock_register, "register_active");
                RenderListClassRoom(context);
            }
            #endregion
        }

        #region Render Classroom
        private void RenderSingleClassRoom(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_classroom>(
               c => c.classroom_id.Equals(Id),
               c => new
               {
                   id = c.classroom_id,
                   title = c.classroom_title,
                   content = c.classroom_content,
                   active = c.classroom_active
               }
               );
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderListClassRoom(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_classroom>(
                null,
                null,
                null,
                null,
                c => new
                {
                    id = c.classroom_id,
                    title = c.classroom_title,
                    content = c.classroom_content,
                    active = c.classroom_active
                }
                );

            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }
        #endregion

        #region Render Register
        private void RenderSingleRegister(HttpContext context, int Id)
        {
            BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
            object obj = bussiness.Get<eng_register>(
               c => c.register_id.Equals(Id),
               c => new
               {
                   id = c.register_id,
                   classroom_id = c.classroom_id,
                   fullname = c.register_fullname,
                   phone = c.register_phone,
                   email = c.register_email,
                   content = c.register_content,
                   date = c.register_date,
                   status = c.register_status,
                   active = c.register_active
               }
               );
            PropertyInfo info = obj.GetType().GetProperty("classroom_id");
            var value = info.GetValue(obj, null);

            IList<object> list = bussiness.List<eng_classroom>(
                null,
                null,
                n => n.classroom_id.Equals(value),
                null,
                n => n.classroom_id
                );
            List<Dictionary<string, object>> listclassroom = bussiness.RenderArrayMultiSelectJSON(EnglishData.ViewClassroom(),new List<object> {value}, "id", "title");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("register", obj);
            dic.Add("classroom", listclassroom);

            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderListRegister(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_register>(
                null,
                null,
                null,
                null,
                c => new
                {
                    id = c.register_id,
                    classroom_id=c.classroom_id,
                    fullname = c.register_fullname,
                    phone=c.register_phone,
                    email = c.register_email,
                    content=c.register_content,
                    date=c.register_date,
                    status=c.register_status,
                    active = c.register_active
                }
                );

            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }
        public void RenderSelectClassroom(HttpContext context)
        {
            BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
            var classroom = bussiness.RenderArrayMultiSelectJSON(EnglishData.ViewClassroom(), null, "id", "title");

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("classroom", classroom);

            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        } 
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}